package com.mresources.resources.service.impl;

import com.mresources.common.dto.bo.UploadFileBo;
import com.mresources.common.dto.vo.FileListVO;
import com.mresources.common.dto.vo.FileVo;
import com.mresources.common.dto.vo.UploadFileVO;
import com.mresources.resources.handler.FileHandler;
import com.mresources.resources.handler.FileHandlerFactory;
import com.mresources.resources.service.FileService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Arrays;

@Service
public class FileServiceImpl implements FileService {
    public static final String UPLOAD = "G:\\upload\\";
    @Resource
    private FileHandlerFactory handlerFactory;

    @Override
    public FileListVO listFiles(String userSeq) {
        FileVo fileVo = FileVo.builder()
                .fileId(11L)
                .deleteBatchNum(null)
                .deleteFlag(0)
                .fileName("fff")
                .filePath("/")
                .isDir(1)
                .userFileId(97L)
                .uploadTime("2021-12-16 22:23:09")
                .build();
        return FileListVO.builder().list(Arrays.asList(fileVo, fileVo)).total(2).build();
    }

    @Override
    public UploadFileVO writeFile(UploadFileBo fileBo, InputStream inputStream) {
        FileHandler fileHandler = handlerFactory.getInstance();
        return fileHandler.fileAppend("", UPLOAD, fileBo, inputStream);
    }

    @Override
    public UploadFileVO createFile(UploadFileBo fileBo) {
        FileHandler fileHandler = handlerFactory.getInstance();
        return fileHandler.createFile("", UPLOAD, fileBo);
    }
}
